package com.terren.newslibrary.service.webview.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.terren.newslibrary.entity.webview.CustomerProjectEntryDb;
import com.terren.newslibrary.service.basic.impl.BasicServiceSupportImpl;
import com.terren.newslibrary.service.webview.IProjectEntryDbService;

@SuppressWarnings("unchecked")
@Service("projectEntryDbService")
public class ProjectEntryDbServiceImpl extends BasicServiceSupportImpl implements IProjectEntryDbService {
	
	private final String SQL_MAPPER = "webview.projectEntryDbMapper.";


	@Override
	public List<CustomerProjectEntryDb> getProjectEntryDbList(CustomerProjectEntryDb param) throws Exception {
		param.setDbType("newsDB");
		List<CustomerProjectEntryDb> list = new ArrayList<CustomerProjectEntryDb>();
		List<CustomerProjectEntryDb> datalist = (List<CustomerProjectEntryDb>) basicDao.findForList(SQL_MAPPER+"getProjectEntryDbList", param);
		Map<String, List<CustomerProjectEntryDb>> map = new HashMap<String, List<CustomerProjectEntryDb>>();
		//把projectId与dbId相同的数据放到同一个map中
		if(CollectionUtils.isNotEmpty(datalist)){
			for(CustomerProjectEntryDb cust: datalist){
				String key = cust.getProjectId()+"_"+cust.getDbId();
				if(map.containsKey(key)){
					map.get(key).add(cust);
				}else{
					List<CustomerProjectEntryDb> tempList = new ArrayList<CustomerProjectEntryDb>();
					tempList.add(cust);
					map.put(key, tempList);
				}
			}
		}
		//将map数据重新放到list中
		Set<String> set = map.keySet();
		for(Iterator<String> iter = set.iterator(); iter.hasNext();){
			CustomerProjectEntryDb pro = new CustomerProjectEntryDb();
			String key = (String)iter.next();
			List<CustomerProjectEntryDb> maplist = (List<CustomerProjectEntryDb>)map.get(key);
			StringBuilder entrydescs = new StringBuilder();
			for(int i=0; i<maplist.size();i++){
				if(i==maplist.size()-1){
					entrydescs.append(maplist.get(i).getEntryDesc());
				}else{
					entrydescs.append(maplist.get(i).getEntryDesc()).append(",");
				}	
			}
			pro.setEntryDescs(entrydescs.toString());
			pro.setProjectId(maplist.get(0).getProjectId());
			pro.setProjectName(maplist.get(0).getProjectName());
			pro.setDbId(maplist.get(0).getDbId());
			pro.setDbName(maplist.get(0).getDbName());
			pro.setDesc(maplist.get(0).getDesc());
			list.add(pro);
		}
		return list;
	}

	@Override
	public Integer saveProjectEntryDb(CustomerProjectEntryDb param) throws Exception {
		param.setDbType("newsDB");
		return (Integer) basicDao.save(SQL_MAPPER+"saveProjectEntryDb", param);
	}

	@Override
	public Integer updateProjectEntryDb(CustomerProjectEntryDb param) throws Exception {
		param.setDbType("newsDB");
		return (Integer) basicDao.save(SQL_MAPPER+"updateProjectEntryDb", param);
	}

	@Override
	public Integer deleteProjectEntryDb(CustomerProjectEntryDb param) throws Exception {
		param.setDbType("newsDB");
		return (Integer)basicDao.save(SQL_MAPPER+"deleteProjectEntryDb", param);
	}

	@Override
	public List<CustomerProjectEntryDb> getEntryList(CustomerProjectEntryDb param) throws Exception {
		param.setDbType("newsDB");
		return (List<CustomerProjectEntryDb>) basicDao.findForList(SQL_MAPPER+"getEntyrIds", param);
	}

	@Override
	public Map<String, List<CustomerProjectEntryDb>> getEntryTree(CustomerProjectEntryDb param) throws Exception {
		param.setDbType("newsDB");
		Map<String, List<CustomerProjectEntryDb>> map = new HashMap<String, List<CustomerProjectEntryDb>>();
		List<CustomerProjectEntryDb> entryList = (List<CustomerProjectEntryDb>) basicDao.findForList(SQL_MAPPER+"getEntyrIds", param);
		if(CollectionUtils.isNotEmpty(entryList)){
			for(CustomerProjectEntryDb entry: entryList){
				String key = entry.getCatagoryid()+"_"+entry.getCatagoryname();
				if(map.containsKey(key)){
					map.get(key).add(entry);
				}else{
					List<CustomerProjectEntryDb> tempList = new ArrayList<CustomerProjectEntryDb>();
					tempList.add(entry);
					map.put(key, tempList);
				}
			}
		}
		return map;
	}
	
}
